goog.provide('ol.layer.CollectionLayer');

goog.require('ol');
goog.require('ol.layer.Layer');
goog.require('ol.layer.Image');
goog.require('ol.source.ImageTransform');

/**
 *
 * @param options {Object}
 * @extends {ol.layer.Layer}
 * @fires ol.render.Event
 * @constructor
 * @api stable
 */
ol.layer.CollectionLayer = function(options){
    ol.layer.Layer.call(this,  (options||{}));

    /**
     * @private
     * @type {Array}
     */
    this.images = [];
    /**
     * @private
     * @type {Object}
     */
    this.images_map = {};
    /**
     * @private
     * @type {string}
     */
    this.projection = options.projection;
    /**
     * @private
     * @type {string}
     */
    this.direction = options.direction;
};

ol.inherits(ol.layer.CollectionLayer, ol.layer.Layer);

/**
 * @function
 * @api stable
 * @param image {ol.layer.Image}
 */
ol.layer.CollectionLayer.prototype.add = function (image) {
    this.get("map").addLayer(image);
    var uid = ol.getUid(image);
    this.images.push(image);
    this.images_map[uid] = image;
};

/**
 * @function
 * @api stable
 * @param image {ol.layer.Image}
 */
ol.layer.CollectionLayer.prototype.del = function (image) {
    this.get("map").removeLayer(image);
    var uid = ol.getUid(image);
    this.images.push(image);
    for(var i = 0; i < this.images.length; i++){
        if(this.images[i] == image) {
            this.images.splice(i,1);
        }
    }
    delete this.images_map[uid];
};

/**
 * @function
 * @api stable
 */
ol.layer.CollectionLayer.prototype.clear = function () {
    for(var i = 0; i < this.images.length; i++){
        this.get("map").removeLayer(this.images[i]);
    }
};

/**
 * @function
 * @api stable
 * @param visible {boolean}
 */
ol.layer.CollectionLayer.prototype.setVisible = function (visible) {
    for(var i = 0; i < this.images.length; i++){
        this.images[i].setVisible(visible);
    }
};

/**
 * @function
 * @api stable
 * @param opacity {number}
 */
ol.layer.CollectionLayer.prototype.setOpacity = function (opacity) {
    for(var i = 0; i < this.images.length; i++){
        this.images[i].setOpacity(opacity);
    }
};

/**
 * @function
 * @api stable
 * @param images {Array}
 */
ol.layer.CollectionLayer.prototype.addLayers = function(images){
    if (images instanceof ol.source.Image) {
        this.addImagesBySource(images);
    } else if (images instanceof ol.layer.Image) {
        this.addImagesByLayers(images);
    } else{
        this.addImages(images)
    }
};

/**
 * @function
 * @api stable
 * @param layers {Array}
 */
ol.layer.CollectionLayer.prototype.addImagesByLayers = function (layers) {
    for(var i = 0; i < layers.length; i++){
        var layer = layers[i];
        this.add(layer);
    }
};

/**
 * @function
 * @api stable
 * @param sources {Array}
 */
ol.layer.CollectionLayer.prototype.addImagesBySource = function (sources) {
    var layers = sources.map(function(source){
        return new ol.layer.Image({
            source: source
        });
    });
    this.addImagesByLayers(layers);
};

/**
 * @function
 * @api stable
 * @param options {Array}
 */
ol.layer.CollectionLayer.prototype.addImages = function (options) {
    var sources = options.map(function(option){
        return new ol.source.ImageTransform({
            url: option.url,
            crossOrigin: '',
            projection: this.projection,
            imageExtent: option.imageExtent,
            targetPolygon: option.targetPolygon,
            direction: this.direction
        });
    }, this);

    this.addImagesBySource(sources);
};